package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.entity.SpuSaleAttrValue;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SaveSpuInfoVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Slf4j
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
    implements SpuInfoService{

    @Autowired
    SpuInfoService spuInfoService;
    @Autowired
    SpuImageService spuImageService;
    @Autowired
    SpuSaleAttrService spuSaleAttrService;
    @Autowired
    SpuSaleAttrValueService spuSaleAttrValueService;

    @Transactional//开启事务
    @Override
    public void saveSpuInfo(SaveSpuInfoVo saveSpuInfoVo) {
        log.info("保存功能");
        //spu_info spu_image  spu_sale_attr  spu_sale_attr_value

        //1保存到 spu_info
        SpuInfo spuInfo = new SpuInfo();
//        spuInfo.setSpuName(saveSkuInfoVo.getSpuName());
//        spuInfo.setDescription(saveSkuInfoVo.getDescription());
//        spuInfo.setCategory3Id(saveSkuInfoVo.getCategory3Id());
//        spuInfo.setTmId(saveSkuInfoVo.getCategory3Id());
        BeanUtils.copyProperties(saveSpuInfoVo, spuInfo);
        spuInfoService.save(spuInfo);

        //2保存到 spu_image
        //StreamAPI
        List<SpuImage> spuImageList = saveSpuInfoVo.getSpuImageList()
                .stream()
                .map((spuImageListDTO) -> {
                    //保存到 spu_image 表中
                    SpuImage spuImage = new SpuImage();
                    BeanUtils.copyProperties(spuImageListDTO, spuImage);
                    spuImage.setSpuId(spuInfo.getId());//回填 spuId
                    return spuImage;
                })
                .collect(Collectors.toList());
        spuImageService.saveBatch(spuImageList);

        //3保存到 spu_sale_attr
        List<SpuSaleAttr> spuSaleAttrList = saveSpuInfoVo.getSpuSaleAttrList()
                .stream()
                .map(item -> {
                    SpuSaleAttr spuSaleAttr = new SpuSaleAttr();
                    BeanUtils.copyProperties(item, spuSaleAttr);
                    spuSaleAttr.setSpuId(spuInfo.getId());//回填 spuId
                    return spuSaleAttr;
                })
                .collect(Collectors.toList());
        spuSaleAttrService.saveBatch(spuSaleAttrList);

        //4保存到 spu_sale_attr_value
        List<SpuSaleAttrValue> spuSaleAttrValueList = saveSpuInfoVo.getSpuSaleAttrList()
                .stream()
                .flatMap((spuSaleAttrListDTO) -> {
                    //每个 spuSaleAttrValueListDTO 有多个属性值 ，进行裂变
                    return spuSaleAttrListDTO.getSpuSaleAttrValueList()
                            .stream()
                            .map(item -> {
                                // 返回 spu_sale_attr_value 对象
                                SpuSaleAttrValue attrValue = new SpuSaleAttrValue();
                                attrValue.setBaseSaleAttrId(item.getBaseSaleAttrId());
                                attrValue.setSaleAttrValueName(item.getSaleAttrValueName());
                                attrValue.setSaleAttrName(spuSaleAttrListDTO.getSaleAttrName());
                                attrValue.setSpuId(spuInfo.getId());//回填商品id
                                return attrValue;
                            });
                })
                .collect(Collectors.toList());
        spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);

    }

/*    @Override
    public Page<SpuInfo> getPage(Long page, Long limit, Long category3Id) {
        //封装分页参数
        Page<SpuInfo> page1 = new Page<>(page,limit);
        //调用方法查询
        Page<SpuInfo> result = this.lambdaQuery().eq(SpuInfo::getCategory3Id, category3Id).page(page1);
        result.getRecords();
        result.getTotal();
        result.getSize();
        result.getCurrent();
        result.getPages();
        return result;
    }*/

}




